﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using ConfExpert.Utility.Grid;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using DevTeamIT;
using Extensions;
using ITaskManager.Forms.Raporlar;
using System;
using System.Data;
using System.Windows.Forms;
using NewLand;

namespace ITaskManager.Forms.IT
{
    public partial class ServisTakip : XForm
    {
        NewLandBarcode newLand = null;
        public ServisTakip()
        {
            InitializeComponent();
        }

        private void ServisTakip_Load(object sender, EventArgs e)
        {
            newLand = new NewLandBarcode(this);
            newLand.CihazEkle(1, Properties.Settings.Default.NewLand1);

            btnKaydet.ItemClick += btnKaydet_ItemClick;
            gridView1.Init("IT_GetServisTakip", Session.SirketId);

            gridView1.Columns["SirketId"].ColumnEdit = RI(Rp.Sirket);
            gridView1.Columns["OnarimTalepEdenKisiId"].ColumnEdit = RI(Rp.V_User0);
            gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
            gridView1.Columns["IT_ServisTakipId"].Caption = "Takip Id";

            gridView1.SetIdentity("IT_ServisTakipId");
            gridView1.DataTable().PrimaryKey = new DataColumn[] {gridView1.DataTable().Columns["IT_ServisTakipId"]};
            gridView1.RowCellStyle += gridView1_RowCellStyle;

            int ndx = 0;
            DataSet d = ExecuteDataSet("GetDistinctValues", "Srv");
            foreach (string col in "MarkaModel,UrunTipi,OnarimaGonderilenFirma,GonderimSekli,Sonuc".Split(",".ToCharArray()))
            {
                GridColumn colUrun = gridView1.Columns[col];
                colUrun.ColumnEdit = GetIntellisensedRI(d.Tables[ndx]);
                ((RepositoryItemGridLookUpEdit)colUrun.ColumnEdit).ProcessNewValue -= ri_NewValue;
                ((RepositoryItemGridLookUpEdit)colUrun.ColumnEdit).ProcessNewValue += ri_NewValue;
                ++ndx;
            }

            GridManager gm;
            gm = GridManager.GetManager(gridView1);
            gm.GridMenu.AddMenu("Barkod Yazdır", BarkodYazdir);
            
            gridView1.AddSummary();
            gridView1.MoveLast();
            gridView1.MakeRowVisible(gridView1.FocusedRowHandle);
            gridView1.BestFitColumns();

            this.FormClosing += ServisTakip_FormClosing;
            gridView1.DataTable().TableNewRow+=ServisTakip_TableNewRow;
        }

        void ServisTakip_TableNewRow(object sender, DataTableNewRowEventArgs e)
        {
            e.Row["Tarih"] = DateTime.Now;
        }

        void gridView1_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            if (e.RowHandle > -1)
            {
                GridView view = sender as GridView;
                if (view.FocusedRowHandle == e.RowHandle)
                    e.Appearance.BackColor = System.Drawing.Color.YellowGreen;
                else
                    e.Appearance.Combine((e.RowHandle & 1) != 0 ? view.Appearance.EvenRow : view.Appearance.OddRow);

                //switch (e.Column.FieldName)
                //{
                //    case "Name":
                //        e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);
                //        break;
                //}
            }

        }

        void ServisTakip_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult dr = MessageBox.Show("Değişiklikleri kaydetmemiş olabilirsiniz; çıkmak istediğinize emin misiniz?", "Uyarı", MessageBoxButtons.YesNo);

            if (dr == DialogResult.No)
                e.Cancel = true;
            else
                e.Cancel = false;
        }

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gridView1.EndEdit();
            gridView1.Update("", "IT_ServisTakip");
            gridView1.VeriyiGuncelle("IT_GetServisTakip", Session.SirketId);
            gridView1.MoveLast();
            gridView1.MakeRowVisible(gridView1.FocusedRowHandle);
            gridView1.BestFitColumns();
        }

        void BarkodYazdir(object sender, EventArgs e) {
            xrServisTakip xst = new xrServisTakip(gridView1.GetFocusedDataRow()["IT_ServisTakipId"].ToInt());
            //
            //xst.Print("NPI2D4184 (HP LaserJet 500 color M551)");
        }
    }
}
